////////////////////////////////////////////////////////////////////////////////
#include "cameraclass.h"


CameraClass::CameraClass():
m_position(D3DXVECTOR3(0.0f, 0.0f, 0.0f)), m_rotation(D3DXVECTOR3(0.0f, 0.0f, 0.0f)), m_up(D3DXVECTOR3(0.0f, 1.0f, 0.0f)), m_lookAt(D3DXVECTOR3(0.0f, 0.0f, 1.0f))
{
	D3DXMatrixLookAtLH(&m_staticViewMatrix, &m_position, &m_lookAt, &m_up);
	D3DXMatrixLookAtLH(&m_viewMatrix, &m_position, &m_lookAt, &m_up);
}

CameraClass::CameraClass(const D3DXVECTOR3& i_pos):
m_position(i_pos), m_rotation(D3DXVECTOR3(0.0f, 0.0f, 0.0f)), m_up(D3DXVECTOR3(0.0f, 1.0f, 0.0f)), m_lookAt(D3DXVECTOR3(0.0f, 0.0f, 1.0f))
{
	D3DXMatrixLookAtLH(&m_staticViewMatrix, &m_position, &m_lookAt, &m_up);
	D3DXMatrixLookAtLH(&m_viewMatrix, &m_position, &m_lookAt, &m_up);
}

void CameraClass::SetPosition(float x, float y, float z)
{
	m_position = D3DXVECTOR3(x,y,z);
}

void CameraClass::SetRotation(float x, float y, float z)
{
	m_rotation = D3DXVECTOR3(x,y,z);
}

void CameraClass::SetPosition(const D3DXVECTOR3& i_pos)
{
	m_position = i_pos;
}

void CameraClass::SetRotation(const D3DXVECTOR3& i_rot)
{
	m_rotation = i_rot;
}

const D3DXVECTOR3& CameraClass::GetPosition() const
{
	return m_position;
}

const D3DXVECTOR3& CameraClass::GetRotation() const
{
	return m_rotation;
}

void CameraClass::Update(InputClass* i_inputMgr)
{

	// view = lookat - pos
	D3DXVECTOR3 ViewVector = (m_lookAt - m_position); 
	D3DXVec3Normalize(&ViewVector, &ViewVector);
	// right vector = up x view 
	D3DXVec3Cross(&m_right,&m_up,&ViewVector);

	float i_elasped = 0.5f; //hardcoded...waiting for timer class!!!

	if(i_inputMgr->IsKeyDown(VK_LEFT))
	{
		m_position	-= i_elasped * CAMERASPEED * m_right;
		m_lookAt	-= i_elasped * CAMERASPEED * m_right;
	}

	if(i_inputMgr->IsKeyDown(VK_RIGHT))
	{
		m_position += i_elasped * CAMERASPEED * m_right;
		m_lookAt += i_elasped * CAMERASPEED * m_right;
	}

	if(i_inputMgr->IsKeyDown(VK_UP))
	{
		m_position += i_elasped * CAMERASPEED * ViewVector;
		m_lookAt += i_elasped * CAMERASPEED * ViewVector;
	}

	if(i_inputMgr->IsKeyDown(VK_DOWN))
	{
		m_position -= i_elasped * CAMERASPEED * ViewVector;
		m_lookAt -= i_elasped * CAMERASPEED * ViewVector;
	}

	if(i_inputMgr->IsKeyDown('q') || i_inputMgr->IsKeyDown('Q'))
	{
		m_position += i_elasped * CAMERASPEED * m_up;
		m_lookAt += i_elasped * CAMERASPEED * m_up;
	}

	if(i_inputMgr->IsKeyDown('a') || i_inputMgr->IsKeyDown('A'))
	{
		m_position -= i_elasped * CAMERASPEED * m_up;
		m_lookAt -= i_elasped * CAMERASPEED * m_up;
	}

	static bool bMoving = false;
	static POINT prevCoord;
	if(i_inputMgr->IsMouseKeyDown(VK_RBUTTON))
	{
		POINT coord;
		GetCursorPos(&coord);
		if(bMoving)
		{

			float motionX = (float)coord.x - (float)prevCoord.x;
			float motionY = (float)coord.y - (float)prevCoord.y;

			D3DXMATRIX yawMatrix;
			D3DXMatrixRotationAxis(&yawMatrix, &m_up, DEG2RAD(motionX * CAMERAANGLESPEED) ); 
			
			D3DXVec3TransformCoord(&ViewVector, &ViewVector, &yawMatrix);
			D3DXVec3TransformCoord(&m_right, &m_right, &yawMatrix); 

			D3DXMATRIX pitchMatrix;
			D3DXMatrixRotationAxis(&pitchMatrix, &m_right, DEG2RAD(motionY * CAMERAANGLESPEED) ); 
			
			D3DXVec3TransformCoord(&ViewVector, &ViewVector, &pitchMatrix);
			D3DXVec3TransformCoord(&m_up, &m_up, &pitchMatrix); 

			m_lookAt = m_position + ViewVector;
			
		}
		prevCoord = coord;
		bMoving = true;
	}
	else
	{
		bMoving = false;
	}
}

void CameraClass::Render()
{
	D3DXMatrixLookAtLH(&m_viewMatrix, &m_position, &m_lookAt, &m_up);
}

void CameraClass::GetViewMatrix(D3DXMATRIX& viewMatrix)
{
	viewMatrix = m_viewMatrix;
}

void CameraClass::GetStaticViewMatrix(D3DXMATRIX& viewMatrix)
{
	viewMatrix = m_staticViewMatrix;
}